/**
 *  Copyright 24.08.2012 oliver.keusemann@googlemail.com
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package de.xelerity.geolessi.srv.impl.mixed;

import java.util.HashMap;
import java.util.Map;
import com.google.appengine.api.memcache.MemcacheService;
import com.google.appengine.api.memcache.MemcacheServiceFactory;
import de.xelerity.geolessi.test.Test;

@SuppressWarnings("unchecked")
public class CacheServiceAppEngine<T> {

	String namespace;
	static Map<Integer, MemcacheService> allCache = new HashMap<Integer, MemcacheService>();

	public CacheServiceAppEngine(String pUniqueNamespace) {
		if (pUniqueNamespace == null)
			pUniqueNamespace = "null";
		namespace = pUniqueNamespace;
		MemcacheService cache = allCache.get(namespace.hashCode());
		if (cache == null) {
			cache = MemcacheServiceFactory.getMemcacheService(namespace);
			allCache.put(namespace.hashCode(), cache);
		}
	}

	public T get(int key) {
		MemcacheService cache = allCache.get(namespace.hashCode());
		return cache == null ? null : (T) cache.get(key);
	}

	public void put(int key, T value) {
		if (value != null) {
			MemcacheService cache = allCache.get(namespace.hashCode());
			if (cache != null)
				cache.put(key, value);
		}
		return;
	}

	public static void main(String[] args) {

		Test.startTest();

		CacheServiceAppEngine<String> cachServiceWR = new CacheServiceAppEngine<String>("String.class");

		cachServiceWR.put(1, "D");
		System.out.println(cachServiceWR.get(1));

		CacheServiceAppEngine<Integer> cachServiceWR1 = new CacheServiceAppEngine<Integer>("Integer.class");

		cachServiceWR1.put(1, 1);
		System.out.println(cachServiceWR1.get(1));

		for (int i = 0; i < Integer.MAX_VALUE; i++) {
			try {
				Thread.sleep(90);
			} catch (InterruptedException e) {
				;
			}
			cachServiceWR.put(i, i + "--" + (i * 2) + "fiewfjiew fjewi fjiew ofjiwe fjeiw fjeiwo fjeio fjewi" + i);
			// System.out.println(" " + cachServiceWR.get(i));
			if (null == cachServiceWR.get(i)) {
				System.out
						.println("----------------------------------------------------------------------------------------------------------");
			}

			int neg = 0, pos = 0;
			if (i > 1000) {
				for (int j = 0; j < i; j++) {
					Object a = cachServiceWR.get(j);
					if (a == null)
						neg++;
					else
						pos++;
				}
				System.out.println(pos + " gefunden. " + neg + " nicht gefunden");
			}

		}

		Test.endTest();
	}
}
